package damuzee.patcha;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class PatchWeb{
	public static void main(String [] args){
		
		String sourceFrom = "E:/workspaces/atmvweb/WebContent/" ; //项目编译路径
		String compileTo = "E:/company/农信/complie/" ;  //打包路径
	
		String projectName = "atmvweb"; //项目名
		String path = "d:/changeLog.txt" ;
		
		String svnName = "ATMV";
		
		String sourceClass= "src";
		String pathPrefix = "/" + sourceClass  + "/src" + projectName + "/";
		
		int cout=0,add=0,mod =0;
		
		try {
			BufferedReader br =new BufferedReader( new FileReader(path));
			String str= null;
			while((str = br.readLine()) != null){
				
				str=str.trim();
				boolean bool = str.startsWith("M") || str.startsWith("A");
				
				if(str.startsWith("A")){
					add++;
				}else if(str.startsWith("M")){
					mod++ ;
				}
				
				String classPathFrom = null ;
				String classPathTo = null ;
				
				if(bool){
					str = str.substring(str.indexOf(svnName)+pathPrefix.length()+1);
					System.out.println(str);
					String suffix = str.endsWith("java") ?".class":str.substring(str.lastIndexOf("."))  ;
					if(str.startsWith(sourceClass)){
								String classPath = str.substring(str.indexOf("/"),str.lastIndexOf("."));
								classPathFrom = sourceFrom +"WEB-INF/classes"+ classPath + suffix ;
								classPathTo = compileTo+projectName+"/WEB-INF/classes" + classPath + suffix;
					}else{
							String classPath = str.substring(str.indexOf("/"));
							classPathFrom = sourceFrom + classPath ;
							classPathTo =compileTo+projectName + classPath;
					}
					System.out.println("FROM :  " + classPathFrom);
					System.out.println("TO :  " + classPathTo);
			 
					BufferedInputStream fis = new BufferedInputStream(new FileInputStream(classPathFrom));
					
					System.out.println(classPathTo.substring(0,classPathTo.lastIndexOf("/")));
					File fout =   new File(classPathTo.substring(0,classPathTo.lastIndexOf("/"))) ;
					if (!fout.exists()) {
						fout.mkdirs();
					}
					BufferedOutputStream fos =new BufferedOutputStream(new FileOutputStream(classPathTo));
					
					byte [] b = new byte[1024];
					
					int len ;
					while ((len = fis.read(b))!=-1){
						fos.write(b, 0, len);
					}
				 
					fos.flush();
					fis.close();
					fos.close();
					System.out.println("-----------------------------------打包成功------------------------------------------------");
					
					cout ++  ;
				}
			}
			br.close();
			System.out.println("++++++打包结束++总更新" + cout +"个文件，其中添加文件总" + add + ",修改的文件总" + mod);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			
		}
	}
}